package com.wf.ew.system.biz.impl;

import com.wf.ew.common.BaseDao;
import com.wf.ew.common.PageResult;
import com.wf.ew.common.constant.UserConstant;
import com.wf.ew.common.utils.Utils;
import com.wf.ew.system.biz.CompanyBiz;
import com.wf.ew.system.model.Company;
import com.wf.ew.system.model.User;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2019-08-17 15:38:50
 * @version ：1.0
 */
@Repository
public class CompanyBizimpl extends BaseDao implements CompanyBiz {

	@Override
	public String list(PageResult<T> page, Company company, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<Company> rm = new BeanPropertyRowMapper<Company>(Company.class);
		
		StringBuffer sql = new StringBuffer("select c.createTime, c.createUser, c.id, c.name, c.person,"
			+" c.phone,c.phone2,c.tel,c.fax,c.address, c.updateTime, c.updateUser");
			sql.append(",suser.name createName from we_company c ");
			sql.append(" inner join sys_user suser on suser.id=c.createUser ");
			sql.append(" where c.del=0 ");
		if(!loginUser.getId().equals(UserConstant.ADMIN_ID)){
			sql.append(" and c.id=:tablecompanyId ");
			map.put("tablecompanyId", loginUser.getCompanyId());
		}
		if(Utils.isNotEmpty(company)){
			if(Utils.isNotEmpty(company.getName())){
				sql.append(" and instr(c.name,:name)>0 ");
				map.put("name", company.getName());
			}
		}
		return jsonPage(getPageQueryList(page, sql.toString(), map, rm));
	}
	
	@Override
	public List<Company> list(Company company, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<Company> rm = new BeanPropertyRowMapper<Company>(Company.class);
		
		StringBuffer sql = new StringBuffer("select c.createTime, c.createUser, c.id, c.name, c.person,"
			+" c.phone,c.phone2,c.tel,c.fax, c.address, c.updateTime, c.updateUser");
			sql.append(",suser.name createName from we_company c ");
			sql.append(" inner join sys_user suser on suser.id=c.createUser ");
			sql.append(" where c.del=0 ");
		if(!loginUser.getId().equals(UserConstant.ADMIN_ID)){
			sql.append(" and c.id=:tablecompanyId ");
			map.put("tablecompanyId", loginUser.getCompanyId());
		}
		if(Utils.isNotEmpty(company)){
//			if(Utils.isNotEmpty(company.getName())){
//				sql.append(" and instr(c.name,:name)>0 ");
//				map.put("name", company.getName());
//			}
		}
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	public Company getById(Company company) {
		RowMapper<Company> rm = new BeanPropertyRowMapper<Company>(Company.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select c.createTime, c.createUser, c.id, c.name, c.person,"
			+" c.phone,c.phone2,c.tel,c.fax, c.address, c.updateTime, c.updateUser ");
			sql.append(",suser.name createName from we_company c ");
			sql.append(" inner join sys_user suser on suser.id=c.createUser ");
			sql.append(" where c.del=0 ");
		sql.append("and c.id=:id ");
		map.put("id", company.getId());
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(), map, rm);
	}

	@Override
	public boolean add(Company company, User loginUser) {
		int count = 0;
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into we_company (createUser,name "
				+",person,phone,phone2,tel,fax,address )");
		sql.append("values (:createUser,:name "
				+",:person,:phone,:phone2,:tel,:fax,:address )");
		map.put("name", company.getName());
		map.put("person", company.getPerson());
		map.put("phone", company.getPhone());
		map.put("phone2", company.getPhone2());
		map.put("tel", company.getTel());
		map.put("fax", company.getFax());
		map.put("address", company.getAddress());
		map.put("createUser", loginUser.getId());
		getMapParameter(sql, map);
		count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean update(Company company, User loginUser) {
		int count = 0;
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update we_company set  ");
		sql.append("name=:name ");
		sql.append(", person=:person, phone=:phone");
		sql.append(", phone2=:phone2, tel=:tel, fax=:fax,address=:address");
		sql.append(", updateUser=:updateUser, updateTime=now() where id=:id");
		map.put("name", company.getName());
		map.put("person", company.getPerson());
		map.put("phone", company.getPhone());
		map.put("phone2", company.getPhone2());
		map.put("tel", company.getTel());
		map.put("fax", company.getFax());
		map.put("address", company.getAddress());
		map.put("updateUser", loginUser.getId());
		map.put("id", company.getId());
		getMapParameter(sql, map);
		count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean delete(Company company, User loginUser) {
		int count = 0;
		Map<String, String> map = null;
		if(company.getIds().length()>0){
			for(String ID : company.getIds().split(",")){
				map  = new HashMap<String, String>();
				if(ID==null || ID.equals("")){ break; }
				StringBuffer sql =new StringBuffer("update we_company set del=1" +
						",updateUser=:updateUser, updateTime=now() " 
						+ " where id =:id ");
				map.put("updateUser", loginUser.getId());
				map.put("id", ID);
				getMapParameter(sql, map);
				count = jdbcTemplate.update(sql.toString(),map);
			}
		}
		return count > 0;
	}
}
